package com.sam.im.heiqun.wxapi;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.nets.PGApi;
import com.sam.im.heiqun.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yuyh.library.utils.EventBusType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, PGApi.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.i(TAG, "onPayFinish, errCode = " + resp.errCode);


        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {
                case -2:
                    //取消
                    ToastUtil.showtoast(WXPayEntryActivity.this, "支付取消！");
                    finish();
                    break;
                case -1:
                    //失败
                    ToastUtil.showtoast(WXPayEntryActivity.this, "支付失败！");
                    finish();
                    Bundle bundle = new Bundle();
                    resp.toBundle(bundle);
                    saveToFile(bundle.toString());
                    break;
                case 0:
                    //成功
                    ToastUtil.showtoast(WXPayEntryActivity.this, "支付成功！");
                    EventBus.getDefault().post(EventBusType.RESULT_PAY_WX_SUCCESS);
                    finish();
                    break;
            }

//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
        }
    }

    public static void saveToFile(String content) {

        File file = new File(Environment.getExternalStorageDirectory(), "pay_log.txt");
        try {

            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

            long length = randomAccessFile.length();
            randomAccessFile.seek(length);
            content += "\r\n";
            randomAccessFile.write(content.getBytes("utf-8"));
            randomAccessFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}